﻿/*
 * TODO:
 * Add code to change color of aircraft for collision avoidance
 * Add code to spawn aircraft outside of radar
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATCSim.Objects
{
    class Aircraft
    {
        Random rand = new Random(DateTime.Now.Second);
        private int _Speed;
        private int _ToSpeed;
        private double _Heading;
        private double _ToHeading;
        private bool _TurnLeft;
        private int _Altitude;
        private int _ToAltitude;
        private int _minSpeed;
        private String _ID;
        private String _DestinationAiportID;
        private String _Control;
        private PointF _Position = new PointF();
        //private bool _CollisionWarning = false;
        private Aircraft _CollideWith = null;
        Brush b1 = Brushes.Black;

        public Aircraft()
        {
            _Position.X = rand.Next(285, 935);
            _Position.Y = rand.Next(30, 680);
            _Heading = rand.Next(0, 360);
            _ToHeading = _Heading;
            _Speed = rand.Next(65, 250);
            _Altitude = rand.Next(750,10000);
            _ToAltitude = _Altitude;
            _ID = "AA239";
            _DestinationAiportID = "MSP";
            _Control = "D";

            _Altitude = 5000;
            _ToAltitude = 5000;
            _ToSpeed = _Speed;
        }
#region Properties
        public PointF Position
        {
            get { return _Position; }
            set { _Position = value; }
        }
        public int MinSpeed
        {
            get { return _minSpeed; }
        }
        public Brush AircraftColor
        {
            set { b1 = value; }
        }
        public int Altitude
        {
            get { return _Altitude; }
        }
        public int ToAltitude
        {
            get { return _ToAltitude; }
            set { _ToAltitude = value; }
        }
        public double ToHeading
        {
            get { return _ToHeading; }
            set { _ToHeading = value; }
        }
        public String DestinationAirport
        {
            get { return _DestinationAiportID; }
            set { _DestinationAiportID = value; }
        }
        public int Speed
        {
            get { return _Speed; }
        }
        public int ToSpeed
        {
            get { return _ToSpeed; }
            set { _ToSpeed = value; }
        }
        public bool TurnLeft
        {
            get { return _TurnLeft; }
            set { _TurnLeft = value; }
        }
        public String ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public double Heading
        {
            get { return _Heading; }
        }
        public Aircraft CollidedWith
        {
            get { return _CollideWith; }
            set { _CollideWith = value; }
        }
#endregion

        public Aircraft(String ID, PointF Pos, int Heading, int Altitude, String Control)
        {
            if (ID.StartsWith("P") | ID.StartsWith("R"))
            {
                _minSpeed = 65;
                _Speed = 85;
            }
            else
            {
                _minSpeed = 150;
                _Speed = 175;
            }
            //TODO: Need to randomly put aircraft on radar but not inside airspace
            _Position.X = Pos.X;
            _Position.Y = Pos.Y;
            _ID = ID;
            _DestinationAiportID = "MSP";
            _Heading = Heading;
            _Altitude = Altitude;
            _ToAltitude = _Altitude;
            _ToHeading = _Heading;
            _Control = Control;
            _ToSpeed = _Speed;
        }
        public void Update()
        {
            //ratio for proper movement Speed/70
            float r = ((float)_Speed / 70f);
            //Polar equation conversion to x,y
            _Position.X += (float)r * (float)Math.Cos((_Heading-90)*(Math.PI/180));
            _Position.Y += (float)r * (float)Math.Sin((_Heading-90)*(Math.PI/180));

            if (_Altitude != _ToAltitude)
            {
                //40feet per second is equal to 800fpm 
                for (int i = 0; i < 40; i++)
                {
                    if (_Altitude == _ToAltitude)
                        break;
                    if (_Altitude > _ToAltitude)
                        _Altitude--;
                    if (_Altitude < _ToAltitude)
                        _Altitude++;
                }
            }
            if (_Heading != _ToHeading)
            {
                //9 deg per 3 second is equal to 360 deg per 2 min
                if (_Heading <= 0)
                    _Heading = 360;
                if (_Heading > 360)
                    _Heading = 0;
                //9 deg per 3 second is equal to 360 deg per 2 min
                for (int i = 0; i < 9; i++)
                {
                    if (_Heading == _ToHeading)
                        break;
                    if (_TurnLeft == true)
                    {
                        _Heading--;
                    }
                    if (_TurnLeft == false)
                    {
                        _Heading++;
                    }
                }
            }
            if (_Speed != _ToSpeed)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (_Speed == _ToSpeed)
                        break;
                    if (_Speed > _ToSpeed)
                        _Speed--;
                    if (_Speed < _ToSpeed)
                        _Speed++;
                }
            }
        }
        public void DrawAirplane(System.Windows.Forms.PaintEventArgs e)
        {
            //Draws Dot and aircraft collision radius
            //if (DrawAircraft == true)
            //{
                e.Graphics.DrawEllipse(new Pen(b1), _Position.X, _Position.Y, 15, 15);
                e.Graphics.FillEllipse(b1, _Position.X + 6, _Position.Y + 6, 3, 3);

                //Draw Aircraft Info Text
                //if (_CollisionWarning == true)
                 //   b1 = Brushes.Red;
                if (Position.X < 935)
                {
                    e.Graphics.DrawString(_ID + " " + _DestinationAiportID + " " + " " + _Control, new Font("Arial", 8), b1, _Position.X + 15, _Position.Y - 5);
                    e.Graphics.DrawString(String.Format("{0:000}", _Altitude / 100) + " " + String.Format("{0:000}", _Speed) + " " + String.Format("{0:000}", _Heading), new Font("Arial", 8), b1, _Position.X + 15, _Position.Y + 5);
                }
                else
                {
                    e.Graphics.DrawString(_ID + " " + _DestinationAiportID + " " + " " + _Control, new Font("Arial", 8), b1, _Position.X - 83, _Position.Y - 5);
                    e.Graphics.DrawString(String.Format("{0:000}", _Altitude / 100) + " " + String.Format("{0:000}", _Speed) + " " + String.Format("{0:000}", _Heading), new Font("Arial", 8), b1, _Position.X - 83, _Position.Y + 5);
                }
            //}
        }
    }
}
